package model
{
	import core.Control;
	import core.Model;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import model.net.handlers.DataRoomHandler;
	import model.net.handlers.DataSystemHandler;
	import model.net.handlers.DataTestHandler;

	public class NetDataParserModel
	{
		private static var instance:NetDataParserModel;
		private var _control:Control = Control.getInstance();
		private var _model:Model = Model.getInstance();
		
		public function NetDataParserModel()
		{
			if (instance)
			{
				throw Error("单列");
			}
			
		}
		
		public static function getInstance():NetDataParserModel
		{
			if (instance == null)
			{
				instance = new NetDataParserModel();
				instance.init();
			}
			return instance;
		}
		
		//---------------------------------------
		
		private var parserFunctionDic:Dictionary;
		
		private function init():void
		{
			parserFunctionDic = new Dictionary();
			intDataHandlers();
		}
		
		public function intDataHandlers():void
		{
			new DataTestHandler();
			new DataRoomHandler();
			new DataSystemHandler();
		}
			
		public function excute(id:int,data:ByteArray):void
		{
			var parserFunction:Function = getRegistFunction(id);
			if (parserFunction != null)
			{
				parserFunction(data);
			}
			else
			{
				//throw Error("网络数据函数没有注册，id号：" + id);
	
				_model.traceManager.out("网络数据函数没有注册，id号：" + id);
			}
		}
		
		public function register(id:int,parserFunction:Function):void
		{
			if (getRegistFunction(id) != null || parserFunction == null)
			{
				throw Error("网络数据解析函数注册重复   或   注册函数为空！");
			}
			parserFunctionDic[id] = parserFunction;	
		}
		
		private function getRegistFunction(id:int):Function
		{
			return parserFunctionDic[id];
		}
	}
}